home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / doc / expr.n < prev    next >
Text File  |  1993-05-03  |  11KB  |  299 lines

  1. '\"
  2. '\" Copyright (c) 1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/expr.n,v 1.3 93/05/03 15:44:45 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .so man.macros
  24. .HS expr tcl 7.0
  25. .BS
  26. '\" Note:  do not modify the .SH NAME line immediately below!
  27. .SH NAME
  28. expr \- Evalue an expression
  29. .SH SYNOPSIS
  30. \fBexpr \fIarg \fR?\fIarg arg ...\fR?
  31. .BE
  32.  
  33. .SH DESCRIPTION
  34. .PP
  35. .VS
  36. Concatenates \fIarg\fR's (adding separator spaces between them),
  37. evaluates the result as a Tcl expression, and returns the value.
  38. .VE
  39. The operators permitted in Tcl expressions are a subset of
  40. the operators permitted in C expressions, and they have the
  41. same meaning and precedence as the corresponding C operators.
  42. Expressions almost always yield numeric results
  43. (integer or floating-point values).
  44. For example, the expression
  45. .DS
  46. \fBexpr 8.2 + 6\fR
  47. .DE
  48. evaluates to 14.2.
  49. Tcl expressions differ from C expressions in the way that
  50. operands are specified.  Also, Tcl expressions support
  51. non-numeric operands and string comparisons.
  52. .SH OPERANDS
  53. .PP
  54. A Tcl expression consists of a combination of operands, operators,
  55. and parentheses.
  56. White space may be used between the operands and operators and
  57. parentheses; it is ignored by the expression processor.
  58. Where possible, operands are interpreted as integer values.
  59. Integer values may be specified in decimal (the normal case), in octal (if the
  60. first character of the operand is \fB0\fR), or in hexadecimal (if the first
  61. two characters of the operand are \fB0x\fR).
  62. If an operand does not have one of the integer formats given
  63. above, then it is treated as a floating-point number if that is
  64. possible.  Floating-point numbers may be specified in any of the
  65. ways accepted by an ANSI-compliant C compiler (except that the
  66. ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in
  67. most installations).  For example, all of the
  68. following are valid floating-point numbers:  2.1, 3., 6e4, 7.91e+16.
  69. If no numeric interpretation is possible, then an operand is left
  70. as a string (and only a limited set of operators may be applied to
  71. it).
  72. .PP
  73. Operands may be specified in any of the following ways:
  74. .IP [1]
  75. As an numeric value, either integer or floating-point.
  76. .IP [2]
  77. As a Tcl variable, using standard \fB$\fR notation.
  78. The variable's value will be used as the operand.
  79. .IP [3]
  80. As a string enclosed in double-quotes.
  81. The expression parser will perform backslash, variable, and
  82. command substitutions on the information between the quotes,
  83. and use the resulting value as the operand
  84. .IP [4]
  85. As a string enclosed in braces.
  86. The characters between the open brace and matching close brace
  87. will be used as the operand without any substitutions.
  88. .IP [5]
  89. As a Tcl command enclosed in brackets.
  90. The command will be executed and its result will be used as
  91. the operand.
  92. .IP [6]
  93. .VS
  94. As a mathematical function whose arguments have any of the above
  95. forms for operands, such as ``\fBsin($x)\fR''.  See below for a list of defined
  96. functions.
  97. .VE
  98. .LP
  99. Where substitutions occur above (e.g. inside quoted strings), they
  100. are performed by the expression processor.
  101. However, an additional layer of substitution may already have
  102. been performed by the command parser before the expression
  103. processor was called.
  104. As discussed below, it is usually best to enclose expressions
  105. in braces to prevent the command parser from performing substitutions
  106. on the contents.
  107. .PP
  108. For some examples of simple expressions, suppose the variable
  109. \fBa\fR has the value 3 and
  110. the variable \fBb\fR has the value 6.
  111. Then the command on the left side of each of the lines below
  112. will produce the value on the right side of the line:
  113. .DS
  114. .ta 6c
  115. \fBexpr 3.1 + $a    6.1
  116. expr 2 + "$a.$b"    5.6
  117. expr 4*[llength "6 2"]    8
  118. expr {word one} < "word $a"    0\fR
  119. .DE
  120. .SH OPERATORS
  121. .PP
  122. The valid operators are listed below, grouped in decreasing order
  123. of precedence:
  124. .TP 20
  125. \fB\-\0\0~\0\0!\fR
  126. Unary minus, bit-wise NOT, logical NOT.  None of these operands
  127. may be applied to string operands, and bit-wise NOT may be
  128. applied only to integers.
  129. .TP 20
  130. \fB*\0\0/\0\0%\fR
  131. Multiply, divide, remainder.  None of these operands may be
  132. applied to string operands, and remainder may be applied only
  133. to integers.
  134. .TP 20
  135. \fB+\0\0\-\fR
  136. Add and subtract.  Valid for any numeric operands.
  137. .TP 20
  138. \fB<<\0\0>>\fR
  139. Left and right shift.  Valid for integer operands only.
  140. .TP 20
  141. \fB<\0\0>\0\0<=\0\0>=\fR
  142. Boolean less, greater, less than or equal, and greater than or equal.
  143. Each operator produces 1 if the condition is true, 0 otherwise.
  144. These operators may be applied to strings as well as numeric operands,
  145. in which case string comparison is used.
  146. .TP 20
  147. \fB==\0\0!=\fR
  148. Boolean equal and not equal.  Each operator produces a zero/one result.
  149. Valid for all operand types.
  150. .TP 20
  151. \fB&\fR
  152. Bit-wise AND.  Valid for integer operands only.
  153. .TP 20
  154. \fB^\fR
  155. Bit-wise exclusive OR.  Valid for integer operands only.
  156. .TP 20
  157. \fB|\fR
  158. Bit-wise OR.  Valid for integer operands only.
  159. .TP 20
  160. \fB&&\fR
  161. Logical AND.  Produces a 1 result if both operands are non-zero, 0 otherwise.
  162. Valid for numeric operands only (integers or floating-point).
  163. .TP 20
  164. \fB||\fR
  165. Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
  166. Valid for numeric operands only (integers or floating-point).
  167. .TP 20
  168. \fIx\fB?\fIy\fB:\fIz\fR
  169. If-then-else, as in C.  If \fIx\fR
  170. evaluates to non-zero, then the result is the value of \fIy\fR.
  171. Otherwise the result is the value of \fIz\fR.
  172. The \fIx\fR operand must have a numeric value.
  173. .LP
  174. See the C manual for more details on the results
  175. produced by each operator.
  176. All of the binary operators group left-to-right within the same
  177. precedence level.  For example, the command
  178. .DS
  179. \fBexpr 4*2 < 7\fR
  180. .DE
  181. returns 0.
  182. .PP
  183. The \fB&&\fP, \fB||\fP, and \fB?:\fP operators have ``lazy
  184. evaluation'', just as in C, 
  185. which means that operands are not evaluated if they are
  186. not needed to determine the outcome.  For example, in the command
  187. .DS
  188. \fBexpr {$v ? [a] : [b]}\fR
  189. .DE
  190. only one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,
  191. depending on the value of \fB$v\fP.  Note, however, that this is
  192. only true if the entire expression is enclosed in braces;  otherwise
  193. the Tcl parser will evaluate both \fB[a]\fR and \fB[b]\fR before
  194. invoking the \fBexpr\fR command.
  195. .SH "MATH FUNCTIONS"
  196. .PP
  197. .VS
  198. Tcl supports the following mathematical functions in expressions:
  199. .DS
  200. .ta 3c 6c 9c
  201. \fBacos\fR    \fBcos\fR    \fBhypot\fR    \fBsinh\fR
  202. \fBasin\fR    \fBcosh\fR    \fBlog\fR    \fBsqrt\fR
  203. \fBatan\fR    \fBexp\fR    \fBlog10\fR    \fBtan\fR
  204. \fBatan2\fR    \fBfloor\fR    \fBpow\fR    \fBtanh\fR
  205. \fBceil\fR    \fBfmod\fR    \fBsin\fR
  206. .DE
  207. Each of these functions invokes the math library function of the same
  208. name;  see the manual entries for the library functions for details
  209. on what they do.  Tcl also implements the following functions for
  210. conversion between integers and floating-point numbers:
  211. .TP
  212. \fBabs(\fIarg\fB)\fI
  213. Returns the absolute value of \fIarg\fR.  \fIArg\fR may be either
  214. integer or floating-point, and the result is returned in the same form.
  215. .TP
  216. \fBdouble(\fIarg\fB)\fR
  217. If \fIarg\fR is a floating value, returns \fIarg\fR, otherwise converts
  218. \fIarg\fR to floating and returns the converted value.
  219. .TP
  220. \fBint(\fIarg\fB)\fR
  221. If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts
  222. \fIarg\fR to integer by truncation and returns the converted value.
  223. .TP
  224. \fBround(\fIarg\fB)\fR
  225. If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts
  226. \fIarg\fR to integer by rounding and returns the converted value.
  227. .PP
  228. In addition to these predifined functions, applications may
  229. define additional functions using \fBTcl_CreateMathFunc\fR().
  230. .VE
  231. .SH "TYPES, OVERFLOW, AND PRECISION"
  232. .PP
  233. All internal computations involving integers are done with the C type
  234. \fIlong\fP, and all internal computations involving floating-point are
  235. done with the C type \fIdouble\fP.
  236. When converting a string to floating-point, exponent overflow is
  237. detected and results in a Tcl error.
  238. For conversion to integer from string, detection of overflow depends
  239. on the behavior of some routines in the local C library, so it should
  240. be regarded as unreliable.
  241. In any case, integer overflow and underflow are generally not detected
  242. reliably for intermediate results.  Floating-point overflow and underflow
  243. are detected to the degree supported by the hardware, which is generally
  244. pretty reliable.
  245. .PP
  246. Conversion among internal representations for integer, floating-point,
  247. and string operands is done automatically as needed.
  248. For arithmetic computations, integers are used until some
  249. floating-point number is introduced, after which floating-point is used.
  250. For example,
  251. .DS
  252. \fBexpr 5 / 4\fR
  253. .DE
  254. returns 1, while
  255. .DS
  256. \fBexpr 5 / 4.0\fR
  257. \fBexpr 5 / ( [string length "abcd"] + 0.0 )
  258. .DE
  259. both return 1.25.
  260. .VS
  261. Floating-point values are always returned with a ``.''
  262. or an ``e'' so that they will not look like integer values.  For
  263. example,
  264. .DS
  265. \fBexpr 20.0/5.0\fR
  266. .DE
  267. returns ``4.0'', not ``4''.  The global variable \fBtcl_precision\fR
  268. determines the the number of significant digits that are retained
  269. when floating values are converted to strings (except that trailing
  270. zeroes are omitted).  If \fBtcl_precision\fR
  271. is unset then 6 digits of precision are used.
  272. To retain all of the significant bits of an IEEE floating-point
  273. number set \fBtcl_precision\fR to 17;  if a value is converted to
  274. string with 17 digits of precision and then converted back to binary
  275. for some later calculation, the resulting binary value is guaranteed
  276. to be identical to the original one.
  277. .VE
  278.  
  279. .SH "STRING OPERATIONS"
  280. .PP
  281. String values may be used as operands of the comparison operators,
  282. although the expression evaluator tries to do comparisons as integer
  283. or floating-point when it can.
  284. If one of the operands of a comparison is a string and the other
  285. has a numeric value, the numeric operand is converted back to
  286. a string using the C \fIsprintf\fP format specifier
  287. \fB%d\fR for integers and \fB%g\fR for floating-point values.
  288. For example, the commands
  289. .DS
  290. \fBexpr "0x03" > "2"\fR
  291. \fBexpr "0y" < "0x12"\fR
  292. .DE
  293. both return 1.  The first comparison is done using integer
  294. comparison, and the second is done using string comparison after
  295. the second operand is converted to the string ``18''.
  296.  
  297. .SH KEYWORDS
  298. arithmetic, boolean, compare, expression
  299.